using System;
using System.ComponentModel;

namespace AntEater.ComponentModel
{
    /// <summary>
    /// Represents an external build property
    /// </summary>
    [Serializable]
    public class ExternalBuildProperty : INotifyPropertyChanged
    {
        /// <summary>
        /// Occurs when the instance or some of his child objects has changed.
        /// </summary>
        public event EventHandler<EventArgs> Changed;

        /// <summary>
        /// Occurs when a property of the instance has changed
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalBuildProperty"/> class.
        /// </summary>
        public ExternalBuildProperty() 
            : this(string.Empty, string.Empty){
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalBuildProperty"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public ExternalBuildProperty(string name) 
            :this (name, string.Empty){
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalBuildProperty"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        public ExternalBuildProperty(string name, string value) {
            this._name = name;
            this._value = value;
        }

        private string _name;

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name {
            get { return _name; }
            set {
                if (_name != value) {
                    _name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }

        private string _value;

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public string Value {
            get { return _value; }
            set {
                if (_value != value) {
                    _value = value;
                    this.OnPropertyChanged("Value");
                } 
            }
        }

        /// <summary>
        /// Called when [changed].
        /// </summary>
        protected void OnChanged() {
            if (this.Changed != null) {
                this.Changed(this, new EventArgs());
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName) {
            if (this.PropertyChanged != null) {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

            this.OnChanged();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString() {
            string result = string.Empty;

            if (!string.IsNullOrEmpty(_name)) {
                result += _name + " = ";
            }

            if (!string.IsNullOrEmpty(_value)) {
                result += _value;
            }

            if (string.IsNullOrEmpty(result)) {
                result = base.ToString();
            }

            return result;
        }
    }
}
